package com.hengxinguotong.hxgtwygsp.ui.supervise.suggestion;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.GridLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.Quarter;
import com.hengxinguotong.hxgtwygsp.model.Year;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseTabFragment;
import com.hengxinguotong.hxgtwygsp.widget.DoubleListPW;
import com.hengxinguotong.hxgtwygsp.widget.QuarterPW;
import com.hengxinguotong.hxgtwygsp.widget.RingView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * TODO 投诉建议
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/4/10 13:20
 */
public class SuggestionFragment extends BaseTabFragment implements SuggestionContract.IView,
        DoubleListPW.OnClickCallback<Year, Quarter> {
    @Inject
    SuggestionContract.IPresenter mPresenter;
    @BindView(R.id.suggestion_quarter)
    TextView suggestionQuarter;
    @BindView(R.id.ring_accept_view)
    RingView ringAcceptView;
    @BindView(R.id.suggestion_accept_container)
    GridLayout suggestionAcceptContainer;
    @BindView(R.id.ring_deal_view)
    RingView ringDealView;
    @BindView(R.id.suggestion_deal_container)
    GridLayout suggestionDealContainer;
    @BindView(R.id.suggestion_accept_num)
    TextView mSuggestionAcceptNum;
    @BindView(R.id.suggestion_dealt)
    TextView mSuggestionDealt;
    @BindView(R.id.suggestion_dealing)
    TextView mSuggestionDealing;
    @BindView(R.id.suggestion_dealt_num)
    TextView mSuggestionDealtNum;
    @BindView(R.id.suggestion_dealing_num)
    TextView mSuggestionDealingNum;
    @BindView(R.id.suggestion_dealt_rate)
    TextView mSuggestionDealtRate;
    @BindView(R.id.suggestion_dealing_rate)
    TextView mSuggestionDealingRate;
    @BindView(R.id.suggestion_satisfied)
    TextView mSuggestionSatisfied;
    @BindView(R.id.suggestion_satisfied_more)
    TextView mSuggestionSatisfiedMore;
    @BindView(R.id.suggestion_satisfied_normal)
    TextView mSuggestionSatisfiedNormal;
    @BindView(R.id.suggestion_dissatisfied)
    TextView mSuggestionDissatisfied;
    @BindView(R.id.suggestion_satisfied_num)
    TextView mSuggestionSatisfiedNum;
    @BindView(R.id.suggestion_more_num)
    TextView mSuggestionMoreNum;
    @BindView(R.id.suggestion_normal_num)
    TextView mSuggestionNormalNum;
    @BindView(R.id.suggestion_dissatisfied_num)
    TextView mSuggestionDissatisfiedNum;
    @BindView(R.id.suggestion_satisfied_rate)
    TextView mSuggestionSatisfiedRate;
    @BindView(R.id.suggestion_more_rate)
    TextView mSuggestionMoreRate;
    @BindView(R.id.suggestion_normal_rate)
    TextView mSuggestionNormalRate;
    @BindView(R.id.suggestion_dissatisfied_rate)
    TextView mSuggestionDissatisfiedRate;
    @BindView(R.id.suggestion_quarter_container)
    RelativeLayout mSuggestionQuarterContainer;

    private List<Year> yearList;

    public SuggestionFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter.bindView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_suggestion;
    }

    @Override
    public void init() {
        mockView();
        yearList = new ArrayList<>();
        String[] quarters = getResources().getStringArray(R.array.array_quarter);
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        int quarterValue = month / 3;
        suggestionQuarter.setText(getString(R.string.patrol_quarter, calendar.get(Calendar.YEAR), quarters[quarterValue]));
        Year year;
        List<Quarter> quarterList;
        Quarter quarter;
        for (int i = 0; i < 3; i++) {
            year = new Year();
            year.setName(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
            year.setValue(calendar.get(Calendar.YEAR));
            quarterList = new ArrayList<>();
            for (int j = 0; j <= quarterValue; j++) {
                quarter = new Quarter();
                quarter.setValue(j);
                quarter.setName(quarters[j]);
                quarterList.add(quarter);
            }
            year.setQuarterList(quarterList);
            yearList.add(year);
            calendar.add(Calendar.YEAR, -1);
            quarterValue = 3;
        }
    }

    private void mockView() {
        mSuggestionAcceptNum.setText(String.format(getString(R.string.suggestion_accept_num),60));
        mSuggestionDealtNum.setText(String.format(getString(R.string.suggestion_num),46));
        mSuggestionDealingNum.setText(String.format(getString(R.string.suggestion_num),14));
        mSuggestionDealtRate.setText(String.format(getString(R.string.suggestion_rate),68));
        mSuggestionDealingRate.setText(String.format(getString(R.string.suggestion_rate),32));
        mSuggestionSatisfiedNum.setText(String.format(getString(R.string.suggestion_num),13));
        mSuggestionMoreNum.setText(String.format(getString(R.string.suggestion_num),10));
        mSuggestionNormalNum.setText(String.format(getString(R.string.suggestion_num),15));
        mSuggestionDissatisfiedNum.setText(String.format(getString(R.string.suggestion_num),22));
        mSuggestionSatisfiedRate.setText(String.format(getString(R.string.suggestion_rate),25));
        mSuggestionMoreRate.setText(String.format(getString(R.string.suggestion_rate),20));
        mSuggestionNormalRate.setText(String.format(getString(R.string.suggestion_rate),30));
        mSuggestionDissatisfiedRate.setText(String.format(getString(R.string.suggestion_rate),25));


        Resources resources = getResources();
        int[] acceptColors = {resources.getColor(R.color.cFE5660), resources.getColor(R.color.c7250FC)};
        int[] dealColors = {resources.getColor(R.color.cFE5660), resources.getColor(R.color.cD1B83B), resources.getColor(R.color.c019FFF), resources.getColor(R.color.c7250FC)};
        float[] acceptValues = {68f, 32f};
        float[] dealValues = {25f, 20f, 30f, 25f};
        ringAcceptView.setColors(acceptColors);
        ringAcceptView.setValues(acceptValues);
        ringDealView.setColors(dealColors);
        ringDealView.setValues(dealValues);
    }

    @OnClick(R.id.suggestion_quarter_container)
    public void onViewClicked(View view) {
        QuarterPW quarterPW = new QuarterPW(getContext(), view.getWidth());
        quarterPW.setOnClickCallback(this);
        quarterPW.initData(yearList);
        quarterPW.showAsDropDown(view);
    }

    @Override
    public void onClickPW(Year year, Quarter quarter) {
        suggestionQuarter.setText(getString(R.string.patrol_quarter, year.getValue(), quarter.getName()));
    }

}
